<template>
<body id="poster">
  <el-form class="login-container" label-position="left"
           label-width="0px">
    <h3 class="login_title">系统登录</h3>
    <el-form-item>
      <el-input type="text" v-model="loginForm.userName" prefix-icon="el-icon-user-solid"
                auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item>
      <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-key"
                auto-complete="off" placeholder="密码" show-password></el-input>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%;border: none" v-on:click="login">登录</el-button>
    </el-form-item>
  </el-form>
</body>
</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        userName: '169000601',
        password: '123456'
      },
      responseResult: [],
      roleMenuResult: []
    }
  },
  methods: {
    login () {
      var _this = this
      console.log(this.$store.state)
      this.$axios
        .post('/login', {
          'userName': this.loginForm.userName,
          'password': this.loginForm.password
        })
        .then(successResponse => {
          if (successResponse.data.code === 200) {
            this.responseResult = successResponse.data.data
            _this.$store.commit('login', _this.responseResult[0])
            this.calculateOnline()
            // _this.$store.commit('calculateOnline', this.calculateOnline())
            this.roleMenuData(this.responseResult[0].roleName)
            console.log(this.responseResult[0])
            // _this.$store.commit('login', _this.loginForm)
            // alert(JSON.stringify(this.loginForm))
            // alert(JSON.stringify(this.responseResult[0]))
            this.$router.replace('/index')

            // if (this.responseResult[0].roleName === '管理员') {
            //   this.$router.replace('/index')
            // } else if (this.responseResult[0].roleName === '学生') {
            //   this.$router.replace('/studentHome')
            // }

            // var path = this.$route.query.redirect
            // this.$router.replace({path: path === '/' || path === undefined ? '/index' : path})
          }
        })
        .catch(failResponse => {
        })
    },
    calculateOnline () {
      var count = 0
      var online = this.$store.state.count

      if (online === null) {
        count += 1
        this.$store.commit('calculateOnline', count)
      } else {
        count = online
        count += 1
        this.$store.commit('calculateOnline', count)
      }
      return count
    },
    roleMenuData (roleName) {
      this.$axios
        .get('/roleMenuResult/' + roleName)
        .then(response => {
          if (response.data.code === 200) {
            this.roleMenuResult = response.data.data
            // alert(JSON.stringify(this.roleMenuResult))
            this.$store.commit('roleMenuResult', this.roleMenuResult)
          }
        })
        .catch(function (error) {
          // 请求失败处理
          console.log(error)
        })
    }
  }
}
</script>
<style>
  #poster {
    background:url("../../assets/log.jpg") no-repeat;
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 0px;
  }
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 90px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: rgba(0, 0, 0,0.5);
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #f1f4f7;
  }

</style>
